//Player Entity
var PlayerEntity = me.ObjectEntity.extend(
{    

        init:function (x, y, settings)
        {
            // call the constructor
            this.parent(x, y , settings);
            
			// character SPEED
			this.walkSpeed = 1;

            // set the walking speed
            this.setVelocity(2.5, 2.5);
			
			// set the walking speed
            this.setFriction(0.5, 0.5);
            
            // adjust the bounding box
            this.updateColRect(13,14,21,6);
            
            // disable gravity
            this.gravity = 0;
            
            //this.firstUpdates = 0;
            this.direction = 'down';
         
			//check when the character is moving
			this.moving = false;
			
			//how many pixels still to move
			this.pixelsToMove = 16;
		 
            // set the display to follow our position on both axis
            me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
            
            this.addAnimation("stand-down", [0]);
            this.addAnimation("stand-left", [8]);
            this.addAnimation("stand-up", [16]);
            this.addAnimation("stand-right", [24]);
            this.addAnimation("down", [1,2,3,4,5,6,7]);
            this.addAnimation("left", [9,10,11,12,13,14,15]);
            this.addAnimation("up", [17,18,19,20,21,22,23]);
            this.addAnimation("right", [25,26,27,28,29,30,31]);
            this.addAnimation("strike-down", [32,33,34,35,36,37]);
			this.addAnimation("strike-left", [38,39,40,41,42]);
            this.addAnimation("strike-up", [43,44,45,46,47]);
            this.addAnimation("strike-right", [48,49,50,51,52]);
        },

        //    Update player position.
        update : function ()
        {
            hadSpeed = this.vel.y != 0 || this.vel.x != 0
			
			//if (me.input.isKeyPressed('left'))
            // {
				//this.animationspeed = me.sys.fps / 20;
                //this.vel.x = -this.walkSpeed * me.timer.tick
               // this.setCurrentAnimation('left')
               // this.direction = 'left'
               // this.moving = true;
            // }
            if (me.input.isKeyPressed('right') && this.moving==false)
            {
               // alert("OI");
				this.animationspeed = me.sys.fps / 20;
                this.setCurrentAnimation('right')
                this.direction = 'right'
                this.moving = true;
                alert("pertou");

            }

            if(this.moving == true) {
                    switch(this.direction) {
                        case 'right' :
                            this.moving = true;
                            var tween = new me.Tween(this.pos).to({x : (this.pos.x+1), y:this.pos.y}, 1);
                            tween.onComplete(function() {
                                this.moving = false;
                                tween.stop();
                            });
                            tween.start();


                            //this.vel.x = 2;
                            //this.pixelsToMove -= 1;
                            //alert("PX: "+this.pixelsToMove);
                           // if(this.pixelsToMove < 0) {
                               // alert("parei com "+this.pixelsToMove);
                               // this.vel.y = (this.walkSpeed * me.timer.tick) +(this.pixelsToMove);
                               // alert("PAREI!");
                               // this.pixelsToMove = 16;
                                //this.moving = false;
                           // }
                            break;
                    }
            }


		
            // check & update player movement
            updated = this.updateMovement();
            
			if (this.vel.y == 0 && this.vel.x == 0)
            {
                this.setCurrentAnimation('stand-' + this.direction)
                {
					if	(this.direction == 'left')
						this.updateColRect(12,14,21,7);
					else if (this.direction == 'right')
						this.updateColRect(13,14,21,6);
					else if (this.direction == 'up')
						this.updateColRect(12,14,21,7);
					else if (this.direction == 'down')
						this.updateColRect(13,14,21,6);
					
					if (hadSpeed) 
					{
                       updated = true
					}
				}
            }
		   
			// check for collision
			res = me.game.collide(this);

            // update animation
            if (updated)
            {
                // update object animation
                this.parent(this);
            }
            return updated;
        }

});

